﻿/************************************************
*Author: KIM
*Create Time: 2/14/2017 16:52:24
*CLR Version: 4.0.30319.42000
*Description:
*
*Update History:
*
***********************************************/

using log4net;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ACC.Extensions.Utilities
{
    public class Log4Helper
    {
        private static ILog _AllLog;
        public static ILog AllLog
        {
            get
            {
                if (_AllLog == null)
                {
                    InitLog4NetConfiguration();
                    _AllLog = log4net.LogManager.GetLogger("Log4Net");
                }
                return _AllLog;
            }
        }

        private static void InitLog4NetConfiguration()
        {
            /*Debug log*/
            log4net.Appender.RollingFileAppender debugAppender = new log4net.Appender.RollingFileAppender();
            debugAppender.File = AppDomain.CurrentDomain.BaseDirectory + "\\_log\\Debug.log";
            debugAppender.DatePattern = "yyyyMMdd";
            debugAppender.AppendToFile = true;
            debugAppender.MaxSizeRollBackups = 10;
            debugAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
            debugAppender.MaximumFileSize = "5MB";
            debugAppender.StaticLogFileName = true;
            debugAppender.Layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %logger - %message%newline");
            debugAppender.ActivateOptions();

            /*Info log*/
            log4net.Appender.RollingFileAppender infoAppender = new log4net.Appender.RollingFileAppender();
            infoAppender.File = AppDomain.CurrentDomain.BaseDirectory + "\\_log\\Info.log";
            infoAppender.DatePattern = "yyyyMMdd";
            infoAppender.AppendToFile = true;
            infoAppender.MaxSizeRollBackups = 10;
            infoAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
            infoAppender.MaximumFileSize = "5MB";
            infoAppender.StaticLogFileName = true;
            log4net.Filter.LevelRangeFilter infofilter = new log4net.Filter.LevelRangeFilter();
            infofilter.LevelMin = log4net.Core.Level.Info;
            infofilter.LevelMax = log4net.Core.Level.Info;
            infoAppender.AddFilter(infofilter);
            infoAppender.Layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %logger - %message%newline");
            infoAppender.ActivateOptions();

            /*Warn Log*/
            log4net.Appender.RollingFileAppender warnAppender = new log4net.Appender.RollingFileAppender();
            warnAppender.File = AppDomain.CurrentDomain.BaseDirectory + "\\_log\\Trace.log";
            warnAppender.DatePattern = "yyyyMMdd";
            warnAppender.AppendToFile = true;
            warnAppender.MaxSizeRollBackups = 10;  //备份log文件的个数最多10个
            warnAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
            warnAppender.MaximumFileSize = "5MB";
            warnAppender.StaticLogFileName = true;
            log4net.Filter.LevelRangeFilter warnfilter = new log4net.Filter.LevelRangeFilter();
            warnfilter.LevelMin = log4net.Core.Level.Warn;
            warnfilter.LevelMax = log4net.Core.Level.Error;
            warnAppender.AddFilter(warnfilter);
            warnAppender.Layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %logger - %message%newline");
            warnAppender.ActivateOptions();

            /*Error Log*/
            log4net.Appender.RollingFileAppender errorAppender = new log4net.Appender.RollingFileAppender();
            errorAppender.File = AppDomain.CurrentDomain.BaseDirectory + "\\_log\\Error.log";
            errorAppender.DatePattern = "yyyyMMdd";
            errorAppender.AppendToFile = true;
            errorAppender.MaxSizeRollBackups = 10;  //备份log文件的个数最多10个
            errorAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
            errorAppender.MaximumFileSize = "5MB";
            //errorAppender.StaticLogFileName = false;
            errorAppender.StaticLogFileName = true;
            log4net.Filter.LevelRangeFilter errorfilter = new log4net.Filter.LevelRangeFilter();
            errorfilter.LevelMin = log4net.Core.Level.Error;
            errorfilter.LevelMax = log4net.Core.Level.Fatal;
            errorAppender.AddFilter(errorfilter);
            //errorAppender.Layout = new log4net.Layout.PatternLayout("%date %logger %message%newline");
            errorAppender.Layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %logger - %message%newline");
            errorAppender.ActivateOptions();

            /*
            //Fatal Log
            log4net.Appender.RollingFileAppender fatalAppender = new log4net.Appender.RollingFileAppender();
            fatalAppender.File = AppDomain.CurrentDomain.BaseDirectory + "\\_log\\05Fatal.log";
            fatalAppender.DatePattern = "yyyyMMdd";
            fatalAppender.AppendToFile = true;
            fatalAppender.MaxSizeRollBackups = 10;  //备份log文件的个数最多10个
            fatalAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
            fatalAppender.MaximumFileSize = "5MB";
            fatalAppender.StaticLogFileName = true;
            log4net.Filter.LevelRangeFilter fatalfilter = new log4net.Filter.LevelRangeFilter();
            fatalfilter.LevelMin = log4net.Core.Level.Fatal;
            fatalfilter.LevelMax = log4net.Core.Level.Fatal;
            fatalAppender.AddFilter(fatalfilter);
            fatalAppender.Layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %logger - %message%newline");
            fatalAppender.ActivateOptions();
            */
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
            hierarchy.Name = "Log4Net";
            hierarchy.Root.AddAppender(debugAppender);
            hierarchy.Root.AddAppender(infoAppender);
            hierarchy.Root.AddAppender(warnAppender);
            hierarchy.Root.AddAppender(errorAppender);
            //hierarchy.Root.AddAppender(fatalAppender);
            hierarchy.Root.Level = log4net.Core.Level.All;
            log4net.Config.BasicConfigurator.Configure(hierarchy);
        }

        public static void Debug(object message, Exception exception = null)
        {
            if (exception == null)
                AllLog.Debug(message);
            else
                AllLog.Debug(message, exception);
        }

        public static void Info(object message, Exception exception = null)
        {
            if (exception == null)
                AllLog.Info(message);
            else
                AllLog.Info(message, exception);
        }

        public static void Trace(object message, Exception exception = null)
        {
            if (exception == null)
                AllLog.Warn(message);
            else
                AllLog.Warn(message, exception);
        }

        public static void Error(object message, Exception exception = null)
        {
            if (exception == null)
                AllLog.Error(message);
            else
                AllLog.Error(message, exception);
        }

        public static void Fatal(object message, Exception exception = null)
        {
            if (exception == null)
                AllLog.Fatal(message);
            else
                AllLog.Fatal(message, exception);
        }
    }

}
